********************************************************************************
* 
* Table 1: Descriptive Statistics on Acquirer, Target, and 
* Other For-Profit Hospitals in 2006 
*
********************************************************************************

cap log close 
log using table1_desc.log, replace 



*******
******* Settings and directories 
*******
** specify an output directory 
local fpath_output "/homes/nber/shruthi-dua51934/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606"

** directory containing the main analytic file
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

** directory containing himss supplemental variables 
local fpath_himss = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/himss/input/"

** survival and readmission data folder 
local fpath_survreadm_dat = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/survreadm/input"


** directory containing physician flows supplemental data 
local fpath_physician_flows = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/physician_flows/output/"

** replication analysis directory 
local fpath_analysis = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/analysis_20230606/"

** prepare the data?
local prep_data = 1

** create summary statistics table  
local summary_stats = 1



*****
***** prepare data for descriptives 
*****
if `prep_data' == 1 {

	*******
	******* read and clean up the hospital-year panel  
	*******
	* bring in the hospital-year panel  
	use "`fpath_build_output'/acq_cleaned_complete_20230606.dta", clear 

	** clean up
	label var hospbd "Total beds"

	* reformat urbancbsa variable: 1 if rural 
	bys id: egen urban2 = max(urbancbsa)
	replace urbancbsa = urban2 
	replace urbancbsa = 1 - urbancbsa 
	label var urbancbsa "Share rural hospitals"

	* drop if missing hrrcode
	drop if missing(hrrcode)

	* numeric variable for the aha id 
	egen id2 = group(id)

	* total cost per adjusted discharge 
	gen adj_costs = totcost / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_costs "Adjusted costs per IP discharge (\$1 millions)" 
	winsor2 adj_costs, cuts(05 95) by(year)

	* total revenue per adjusted discharge 
	gen adj_revenue = income / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_revenue "Adjusted revenue per IP discharge (\$1 millions)"
	winsor2 adj_revenue, cuts(05 95) by(year)

	* log costs and revenue 
	gen ladjcosts_w = log((adj_costs_w))
	label var ladjcosts_w "Log(costs/bed)"

	gen ladjrev_w = log((adj_revenue_w))
	label var ladjcosts_w "Log(revenue/bed)"

	* price
	winsor2 dafny_price, cuts(05 95) by(year)
	gen ldafny_price05 = log(dafny_price_w)
	label var ldafny_price05 "log(Dafny price index)"

	* winsorize profit margin in each year
	winsor2 profit_margin, cuts(05 95) by(year) label

	* capital investment 
	replace capinv_tot = capinv_tot/1000000
	label var capinv_tot "Capital investment \$millions"

	* winsorize and log  capital investment
	winsor2 capinv_tot, cuts(05 95) by(year) label
	gen lw_capinv_tot = log(1+capinv_tot_w)
	label var lw_capinv_tot "log(1+winsorized capital investment)"

	* scale FTE counts by beds 
	gen fte_per_bed = fte/hospbd
	label var fte_per_bed  "Full time employees per bed"

	* create log(fte)
	gen logfte = log(fte)
	label var logfte "log(FTE)"
	label var fte "Full Time Employees"
}


*******
******* Descriptive stats 
*******
******* 
eststo clear 
if `summary_stats' == 1 {
	
	cap est sto clear
	cap drop _merge 
	
	preserve 
	
	* keep only year 2006
	keep if year == 2006
	
	* specify the column order on the summary stats table 
	gen order2 = . 
	replace order2 = 1 if order == 1 & year == 2006 & !missing(order)
	replace order2 = 2 if order == 3 & year == 2006 & !missing(order)
	replace order2 = 3 if missing(order) & year == 2006 & forprofit == 1
	label define orderlab2 1 "Acquirer '06" 2 "Target '06" 3 "Other for-profit '06" 
	label values order2 orderlab2 

	* bring in historical risk-adjusted survival rates for all cohorts except hip/knee replacement 
	cap drop _merge 
	foreach cond in ami chf pnu stk hip {
		merge 1:1 id year using "`fpath_survreadm_dat'/`cond'100_acq.dta", nogen keepusing(rhdx_`cond'_fe shdx_`cond'_fe) keep(master match)
	}

	* calculate mean survival rates across all cohorts 
	egen shdx_mean = rowmean(shdx_*)
	egen rhdx_mean = rowmean(rhdx_*)
	label var shdx_mean "Mean survival rate"
	label var rhdx_mean "Mean readmission rate"
	
	* bring in the share of hospitals with Acquirer EMR
	tempfile data06
	save `data06'
	insheet using "`fpath_himss'/../output/count_vendors.csv", clear 
	tostring id , replace 
	keep year id vendor category  
	keep if year == 2006
	
	* count hospitals with at least 1 acquirer EMR application (EMR or HIM)
	gen acq_emr = (vendor == "Acq EMR")
	collapse (sum) acq_emr, by(id year) fast
	replace acq_emr  = (acq_emr > 0) 
	label var acq_emr "Share with acquirer EMR vendor"
	merge 1:1 id year using `data06'


	* bring in the charges-weighted physician churn rate 
	merge 1:1 id year using "`fpath_physician_flows'/physician_flows_data", keepusing(wcost_churn) gen(m)
	label var wcost_churn "Charges weighted physician churn"

	* adjust the Dafny price index
	egen mean_dafny_price05 = mean(dafny_price_w)
	gen adj_dafny_price05 = 100*(dafny_price_w/mean_dafny_price05)
	label var adj_dafny_price05 "Price Index (Mean = 100)"
	
	* re-scale the adjusted costsa and revenue to be $ per discharge, and not millions
	replace adj_costs_w = adj_costs_w * 1000000
	label var adj_costs_w "Costs/IP discharge"
	replace adj_revenue_w = adj_revenue_w * 1000000
	label var adj_revenue_w "Revenue/IP discharge"
	
	* vars we want to summarize 	
	local varlist "hospbd urbancbsa acq_emr wcost_churn profit_margin_w adj_dafny_price05 adj_costs_w adj_revenue_w capinv_tot_w fte shdx_mean rhdx_mean "
	
	* make summary statistics
	bys order2: eststo: quietly estpost sum `varlist' 	

	esttab , replace main(mean) aux(sd) nodepvar label nonote ///
	title("Hospital system summary statistics, 2006") ///
	addnotes("Profit margins winsorized at .05 and .95." "Capital investment is measured in millions." "Survival rates are calculated across AMI, ischemic stroke, hip fracture, and pneumonia patient cohorts." "Standard deviations in parentheses.") 
	
	esttab using "`fpath_output'/Table1_desc_20230606.csv", replace main(mean) aux(sd) nodepvar label nonote title("Hospital system summary statistics, 2006") addnotes("Profit margins winsorized at .05 and .95." "Capital investment is measured in dollars." "Survival rates are calculated across AMI, ischemic stroke, hip fracture, and pneumonia patient cohorts." "Standard deviations in parentheses.") 


	restore 
}





log close 










